package cn.com.cluster.loadbalance;

import cn.com.common.Invocation;
import cn.com.common.model.ProbeModel;
import cn.com.rpc.invoke.Invoker;

import java.util.List;

/**
 * 基于服务提供者数量取模的负载均衡算法
 *
 * @author jiaming
 */
public class HashLoadBalance extends AbstractLoadBalance {

    @Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, Invocation invocation) {
        int size = invokers.size();
        Object[] args = invocation.getArgs();
        ProbeModel params = (ProbeModel) args[0];
        String key = params.getKey();
        int hash = key.hashCode();
        int index = hash % size;
        return invokers.get(index);
    }

}
